package com.lovo.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.commom.CscRemote.ICscOrderInfoRemote;
import com.lovo.commom.CscRemote.ICscUserOrderRemote;
import com.lovo.commom.WebRemote.IWebProductRemote;
import com.lovo.commom.dto.OrderPiCiDto;
import com.lovo.commom.psc.IPscOrderInfoRemote;
import com.lovo.sys.bean.*;
import com.lovo.sys.dto.OrderDto;
import com.lovo.sys.mapper.*;
import com.lovo.sys.service.ISupplyService;
import com.lovo.sys.vo.SupplyOrderInfoVo;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SupplyServiceImpl implements ISupplyService {
    @Autowired
    private ISupplyMapper ISupplyMapper;
    @Autowired
    private IPscOrderInfoRemote pscOrderInfoRemote;

    @Autowired
    private IOrderInfoMapper orderInfoMapper;
    @Autowired
    private ISupplyProductMapper supplyProductMapper;
    @Autowired
    private ISupplyInfoMapper supplyInfoMapper;
    @Autowired
    private IInventoryRecordsMapper iInventoryRecordsMapper;
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private IProductInfoMapper productInfoMapper;
    @Autowired
    private IWebProductRemote webProductRemote;


    @Override
    public IPage<SupplyBean> findAll(Integer pageNum) {
        return ISupplyMapper.selectPage(new Page<>(pageNum, 5), null);
    }

    @Override
    public List<SupplyOrderInfoVo> findById(String idNum) {
        //查询供货订单
        return ISupplyMapper.findById(idNum);
    }

    @Override
    public List<SupplyProductBean> findByOrderInfoId(String orderInfoId) {
        return ISupplyMapper.findByOrderInfoId(orderInfoId);
    }

    @Override
    public void tuiHuo(String idNum) {
        ISupplyMapper.tuiHuo(idNum);

        SupplyOrderInfoVo supplyOrderInfo = ISupplyMapper.findByIdNum(idNum);

        InventoryRecordsBean jilu = new InventoryRecordsBean();
        //设置库存记录的增加数量
        jilu.setNum("-" + supplyOrderInfo.getNum());
        //设置库存记录的商品名【规格】
        jilu.setProductName(supplyOrderInfo.getProductName() + "【" + supplyOrderInfo.getSpecs() + "】");
        iInventoryRecordsMapper.insert(jilu);
        //减少库存
        QueryWrapper qw3 = new QueryWrapper();
        qw3.eq("p_productName", supplyOrderInfo.getProductName());
        ProductBean product = productMapper.selectOne(qw3);
        product.setNum(product.getNum() - supplyOrderInfo.getGoodsNum());
        productMapper.updateById(product);
        //减少明细
        QueryWrapper qw4 = new QueryWrapper<>();
        qw4.eq("pi_productName", supplyOrderInfo.getProductName());
        qw4.eq("pi_specs", supplyOrderInfo.getSpecs());
        ProductInfoBean productInfo = productInfoMapper.selectOne(qw4);
        productInfo.setInventory(productInfo.getInventory() - supplyOrderInfo.getGoodsNum());
        productInfoMapper.updateById(productInfo);

        //远程供应商的订单状态
        pscOrderInfoRemote.updateStatusByIdNum(idNum, 2);

        //远程调用前端商品库存数量
        webProductRemote.inKu(supplyOrderInfo.getProductName(), supplyOrderInfo.getSpecs(), -supplyOrderInfo.getGoodsNum());
    }


    @RabbitListener(queues = "queuePscOrderPIci")
    public void add(String json) {
        if (json != null) {
            OrderDto orderInfo =
                    JSONObject.parseObject(json, OrderDto.class);
            System.err.println(orderInfo);

            //添加批次订单
            OrderInfoBean orderInfoBean = new OrderInfoBean();
            orderInfoBean.setOrderIdNum(orderInfo.getOrderIdNum());//供货主订单编号
            orderInfoBean.setIdNum(orderInfo.getIdNum());//供货批次编号
            orderInfoBean.setOrderPrice(orderInfo.getPrice());//订单金额
            orderInfoBean.setGoodsNum(orderInfo.getNum());//批次的总数量数量
            orderInfoMapper.insert(orderInfoBean);

            QueryWrapper<SupplyInfoBean> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("fk_spIdNum", orderInfo.getProductId());

            List<SupplyInfoBean> list = supplyInfoMapper.selectList(queryWrapper);
            if (list.size() == 0) {
                SupplyInfoBean supplyInfoBean = new SupplyInfoBean();
                supplyInfoBean.setSpIdNum(orderInfo.getProductId());//商品编号
                supplyInfoBean.setSpecs(orderInfo.getSpecs());
                supplyInfoBean.setNum(orderInfo.getNum());
                supplyInfoBean.setImgPath(orderInfo.getImgPath());
                supplyInfoMapper.insert(supplyInfoBean);
            }
            for (SupplyInfoBean su : list) {
                if (su.getSpecs().equals(orderInfo.getSpecs())) {
                    su.setNum(su.getNum() + orderInfo.getNum());
                    supplyInfoMapper.updateById(su);
                } else {
                    SupplyInfoBean supplyInfoBean = new SupplyInfoBean();
                    supplyInfoBean.setSpIdNum(orderInfo.getProductId());//商品编号
                    supplyInfoBean.setSpecs(orderInfo.getSpecs());
                    supplyInfoBean.setNum(orderInfo.getNum());
                    supplyInfoMapper.insert(supplyInfoBean);
                }
            }


            SupplyProductBean supplyProductBean = new SupplyProductBean();
            supplyProductBean.setIdNum(orderInfo.getProductId());//商品编号
            supplyProductBean.setOrderNum(orderInfoBean.getIdNum());//供货批次编号
            supplyProductBean.setProductName(orderInfo.getProductName());//商品名
            supplyProductBean.setType(orderInfo.getType());//商品类型
            supplyProductBean.setNum(orderInfo.getNum());//商品数量
            supplyProductBean.setBuyPrice(orderInfo.getBuyPrice());//商品价格
            supplyProductBean.setTotalPrice(orderInfo.getBuyPrice() * orderInfo.getNum());//总价
            supplyProductMapper.insert(supplyProductBean);
        }
    }

    @RabbitListener(queues = "queuePiCiZhu")
    public void addSupply(String json) {
        OrderPiCiDto orderPiCiDto = JSONObject.parseObject(json, OrderPiCiDto.class);
        System.out.println(orderPiCiDto);
        QueryWrapper<SupplyBean> qw = new QueryWrapper<>();
        qw.eq("s_idNum", orderPiCiDto.getIdNum());
        SupplyBean supplyBean = ISupplyMapper.selectOne(qw);
        if (supplyBean == null) {
            SupplyBean p = new SupplyBean();
            p.setSuppliersName(orderPiCiDto.getVendorName());
            p.setOrderDate(orderPiCiDto.getDate());
            p.setOrderStatus(orderPiCiDto.getStatus());
            p.setIdNum(orderPiCiDto.getIdNum());
            p.setTotalNum(orderPiCiDto.getTotalNum());
            ISupplyMapper.insert(p);
        }
    }


}
